// 初始加载时同步开关状态
document.addEventListener('DOMContentLoaded', () => {
  const toggleInsert = document.getElementById('toggleInsert');

  // 读取存储状态
  chrome.storage.sync.get(['insertEnabled'], (result) => {
    toggleInsert.checked = result.insertEnabled || false;
  });

  // 保存按钮事件
  document.getElementById('insertContainer').addEventListener('click', (e) => {
    // if (!e.target.closest('label')) {
      const checkbox = document.getElementById('toggleInsert');
      checkbox.checked = !checkbox.checked;
      const isEnabled = checkbox.checked
      chrome.storage.sync.set({ insertEnabled: isEnabled }, () => {
      // 通知所有页面开启或关闭对应功能
      chrome.tabs.query({ status: 'complete' }, (tabs) => {
        tabs.forEach(tab => {
          chrome.tabs.sendMessage(tab.id, { action: 'toggleInsert', enabled: isEnabled });
        });
      });
    });
   // }
  });
});

